package com.bw.journal.insert

import android.text.TextUtils
import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.library_mvi.ui.BaseMviActivity
import com.bawei.module_journal.R
import com.bawei.module_journal.databinding.ActivityJournalInsertBinding
import com.bawei.module_journal.insert.JournaInsertIent
import com.bawei.module_journal.insert.JournalInsertState
import com.bawei.module_journal.insert.entity.RequestEntity
import com.bawei.module_journal.list.JournalState
import com.blankj.utilcode.util.ToastUtils
import com.bw.journal.list.JournalListViewModel

import kotlinx.coroutines.launch

@Route(path = "/journal/JournalInsertActivity")
class JournalInsertActivity : BaseMviActivity<ActivityJournalInsertBinding>() {
    private lateinit var viewModel:JournalInsertViewModel
    var type = 0
    private val fragment = JounalTypeFragment()


    fun refreshView(){
        var tile = ""
        var nowTitle = ""
        var nextTitle = ""
        if (type==1){
            tile = "日报"
            nowTitle = "今日工作总结"
            nextTitle = "明日工作计划"
        }else if (type == 2){
            tile = "周报"
            nowTitle = "本周工作总结"
            nextTitle = "下周工作计划"
        }else if (type == 3){
            tile = "月报"
            nowTitle = "本月工作总结"
            nextTitle = "下月工作计划"
        }
        viewDataBinding.journalInsertTitle.text = tile
        viewDataBinding.journalInsertNowTitle.text = nowTitle
        viewDataBinding.journalInsertNextTitle.text = nextTitle
    }

    override fun initView() {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        transaction.replace(R.id.journal_insert_frame,fragment).commit()
        viewDataBinding.journalInsertPage.backImg.setOnClickListener { finish() }
        viewDataBinding.journalInsertPage.titleTv.text = "工作汇报"
        viewDataBinding.journalInsertSub.setOnClickListener {
            if (TextUtils.isEmpty(viewDataBinding.journalInsertNowValue.text)){
                ToastUtils.showShort("请完整填写计划")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(viewDataBinding.journalInsertNextValue.text)){
                ToastUtils.showShort("请完整填写计划")
                return@setOnClickListener
            }

            //工作日志的上传
            val requestEntity =
                RequestEntity(type, viewDataBinding.journalInsertNowValue.text.toString(),
                    viewDataBinding.journalInsertNowValue.text.toString(),
                    viewDataBinding.journalInsertNextValue.text.toString(),
                    viewDataBinding.journalInsertNextValue.text.toString(),
                    "王女士",
                    System.currentTimeMillis() / 1000);
            lifecycleScope.launch {
                viewModel.intents.send(JournaInsertIent.LoadJournalIntent(requestEntity))
            }
        }

    }

    override fun handleState() {
        lifecycleScope.launch {
            viewModel.states.collect{
                when(it){
                    is JournalInsertState.InsertJournalSuccessState ->{
                        ToastUtils.showShort("提交成功")
                        finish()
                    }
                    is JournalInsertState.InsertJournalErrorState ->{
                        ToastUtils.showShort("提交失败")
                        finish()
                    }
                    is JournalInsertState.Init -> {}
                }

            }
        }
    }




    override fun getLayout(): Int {
        return R.layout.activity_journal_insert
    }

    override fun generateViewModel() {
        viewModel = ViewModelProvider(this).get(JournalInsertViewModel::class.java)
    }
}